package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;


@Service
public class SetmealServiceimpl implements SetmealService {
    @Autowired
    private SetmealMapper  setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper  setmealDishMapper;
    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO getSetmealById(long id) {
        Setmeal setmeal=setmealMapper.getSetmealById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        Category category = categoryMapper.getCategoryById(setmeal.getCategoryId());
        //设置分类名称
        setmealVO.setCategoryName(category.getName());
        //TODO 套餐根据id查询，设置套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(setmeal.getId());
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    public void addSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.addSetmeal(setmeal);
        //TODO 处理setmealDishes
        if (!setmealDTO.getSetmealDishes().isEmpty()){
            Setmeal setmeal1 = setmealMapper.getSetmealByName(setmealDTO.getName());
            List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
            for (SetmealDish dish : dishes) {
                dish.setSetmealId(setmeal1.getId());
            }
            setmealDishMapper.addSetmealDish(dishes);
        }

    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     */
    public void changeStatus(Integer status, long id) {
        Setmeal setmeal = setmealMapper.getSetmealById(id);
        setmeal.setStatus(status);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        setmealMapper.changeStatus(setmeal);
    }

    /**
     * 批量删除
     * @param ids
     */
    public boolean deleteByIds(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        if (list.isEmpty()){
            return false;
        }
        setmealMapper.deleteByIds(list);
        return true;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    public void editSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal =new Setmeal();
        setmeal=setmealMapper.getSetmealById(setmealDTO.getId());
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        //TODO 套餐根据id查询，设置套餐和菜品的关联关系
        //删除原有的关联关系
        setmealDishMapper.deleteById(setmeal.getId());
        if (!setmealDTO.getSetmealDishes().isEmpty()){
            List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
            for (SetmealDish dish : dishes) {
                dish.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.addSetmealDish(dishes);
        }

        setmealMapper.editSetmeal(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
